package com.liangtian.mdm.base.filter;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.base.filter </p>
 * <p>Project Name: dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-04-11 10:42</p>
 * <p>Description : 全局拦截器，用于设置前台的根路径</p>
 *
 * @author : Administrator
 * @version :  1.0
 */
public class GlobalInterceptor extends HandlerInterceptorAdapter {

    private final static Properties props = new Properties();

    private static final String CONFIG_FILE = "config.properties";

    private static String IMAGE_DOMAIN;

    static {
        InputStream is = GlobalInterceptor.class.getClassLoader().getResourceAsStream(CONFIG_FILE);
        try {
            props.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        IMAGE_DOMAIN = props.getProperty("imgPath");
    }

    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
//        String basePath = request.getContextPath();
//        request.setAttribute("basePath", basePath);
//        request.getSession(true).setAttribute("basePath", basePath);
//        request.setAttribute("imgPath", IMAGE_DOMAIN);
        return true;
    }

}
